package com.cindata

import com.cindata.dictionary.*

class RiskWarningConfig
{
    String name
    String description
    // 名称
    String code
    // 唯一代码
    Boolean isAuto
    // 触发方式
    String duration
    // 更新频次：周末,月末,季末,年末
    Workflow workflow
    // 工作流

    // 房产条件
    CollateralType collateralType
    EstateStatusDict estateStatus
    BuildingTypeDict buildingType
    Cashability cashability

     // 贷款条件
    RiskWarningLevel riskWarningLevel
    MortgageStatusDict mortgageStatus
    MortgageType mortgageType
    String contactType

    // 借款人条件
    Double creditScoreStart
    Double creditScoreEnd

    // 小区条件
    City city
    District district

    // 机构条件
    String operatingState
    String creditLevel
    ProjectRiskType riskType

    String type
    RiskWarningTypeDict warningType
    Integer status = 1  // 0: 禁用  1：启用

    Integer limitCounts
    // 最大条数
    Date lastExcuteTime
    // 最后执行时间
    String sourceClassType
    // 借款人，押品，贷款，小区
    Boolean isAndRelation = true
    // 规则关系，true: 条件同时满足， false: 某一条件满足

    String chartType
    // 图表类型
    String chartSql
    // 图表数据sql
    Integer chartStatus = 1  // 0: 禁用  1：启用
    // 是否启用图表
    Integer chartSequence
    // 图表顺序
    String chartDescription
    // 图表顺序

    Account bankAccount

    Date createdDate = new Date()
    Date modifiedDate = new Date()
    User createBy
    User modifyBy

    String sourceAction

    static hasMany = [rules: RiskWarningConfigRule, logs: RiskWarningConfigLog]

    static constraints = {
        name maxSize: 50, comment: '名称'
        description maxSize: 200, nullable: true, blank: true, comment: '描述'
        code maxSize: 32, nullable: false, comment: '唯一代码'
        code nullable: true, blank: true
        isAuto nullable: true, blank: true, comment: '触发方式'
        duration maxSize: 10, nullable: true, blank: true, comment: '更新频次'
        duration inList: ['minute', 'hour', 'day', 'week', 'month', 'quarter', 'year']
        workflow nullable: true, blank: true, comment: '工作流'

        createdDate nullable: true, blank: true
        modifiedDate nullable: true, blank: true
        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true

        type maxSize: 20, nullable: false, blank: false
        type inList: ['监测', '预警']
        warningType nullable: true, blank: true
        status nullable: true, blank: true

        // 押品条件
        collateralType nullable: true, blank: true
        estateStatus nullable: true, blank: true
        buildingType nullable: true, blank: true
        cashability nullable: true, blank: true

        // 贷款条件
        riskWarningLevel nullable: true, blank: true
        mortgageStatus nullable: true, blank: true
        mortgageType nullable: true, blank: true
        contactType maxSize: 20, nullable: true, blank: true
        contactType inList: ['对私', '企业']

        // 借款人条件
        creditScoreStart nullable: true, blank: true
        creditScoreEnd nullable: true, blank: true

        // 小区条件
        city nullable: true, blank: true
        district nullable: true, blank: true

        // 机构条件
        operatingState nullable: true, blank: true
        operatingState inList: ['存续', '歇业', '清算', '在业', '吊销', '注销', '迁入', '迁出', '停业']
        creditLevel nullable: true, blank: true
        riskType nullable: true, blank: true

        limitCounts nullable: true, blank: true
        lastExcuteTime nullable: true, blank: true
        sourceClassType maxSize: 20, nullable: true, blank: true
        sourceClassType inList: ['押品', '贷款', '借款人', '小区', '机构']
        isAndRelation nullable: true, blank: true

        chartType maxSize: 20, nullable: true, blank: true
        chartType inList: ['柱状图', '饼状图', '折线图']
        chartSql nullable: true, blank: true
        chartSql type: "text"
        chartStatus nullable: true, blank: true
        chartSequence nullable: true, blank: true
        chartDescription maxSize: 100, nullable: true, blank: true

        bankAccount nullable: true, blank: true
        sourceAction nullable: true, blank: true

    }

    static mapping = {
        cache true
        autowire true
    }

    def springSecurityService
    def beforeUpdate()
    {
        modifiedDate = new Date()
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
    }

    def beforeInsert()
    {
        def username = springSecurityService?.getPrincipal()?.username
        createBy = User.findByUsername(username)
        bankAccount = createBy?.account
    }
}
